<template>
	<view>
		<u-navbar title="发布商品" :background="{ background: '#a0cfff' }" title-color="#FFFFFF"></u-navbar>
		<view class="u-m-15">
			<u-form :model="form" ref="uForm" label-width="150">
				<u-form-item label="商品标题">
					<u-input v-model="form.title" />
				</u-form-item>
				<u-form-item label="商品类别">
					<u-input type="select" v-model="form.type" @click="typeClick" />
				</u-form-item>
				<u-form-item label="商品描述">
					<u-input type="textarea" v-model="form.content" />
				</u-form-item>
				<u-form-item label="商品原价">
					<u-input v-model="form.original" />
				</u-form-item>
				<u-form-item label="商品价格">
					<u-input v-model="form.price" />
				</u-form-item>
				<u-form-item label="数量">
					<u-number-box v-model="form.shopNum"></u-number-box>
				</u-form-item>
				<u-form-item label="商品品质">
					<u-input type="select" v-model="form.quality" @click="qualityClick" />
				</u-form-item>
				<u-form-item label-position="top" label="商品图片" prop="photo" label-width="150">
					<u-upload ref="uUpload" :action="action" :file-list="form.fileList" width="160" height="160"
						@on-remove="onRemove" @on-Success="onSuccess"></u-upload>
				</u-form-item>
			</u-form>
			<u-button type="error" @click="sumbit()">提交</u-button>
		</view>
		<u-select v-model="typeShow" :list="typeList" :default-value="typeDefault" @confirm="typeConfirm"></u-select>
		<u-select v-model="qualityShow" :list="qualityList" :default-value="qualityDefault" @confirm="qualityConfirm">
		</u-select>
	</view>
</template>

<script>
	import helper from '../../common/helper.js'
	export default {
		data() {
			return {
				action: 'http://localhost:9000/eduoss/imgFile',
				typeShow: false,
				qualityShow: false,
				form: {
					title: '',
					type: '',
					typeCode: '',
					content: '',
					original: '',
					price: '',
					shopNum: 0,
					quality: '',
					qualityCode: '',
					fileList: []
				},
				fileImage: [],
				typeList: [],
				typeDefault: [0],
				qualityList: [{
						value: '1',
						label: '十'
					},
					{
						value: '2',
						label: '九'
					},
					{
						value: '3',
						label: '七'
					},
					{
						value: '4',
						label: '五'
					},
					{
						value: '5',
						label: '三'
					},
					{
						value: '6',
						label: '一'
					},
					{
						value: '10',
						label: '零'
					}
				],
				qualityDefault: [0]
			}
		},
		created() {
			this.getTab()
		},
		methods: {
			typeClick() {
				this.typeShow = !this.typeShow
			},
			qualityClick() {
				this.qualityShow = !this.qualityShow
			},
			qualityCreat() {
				let that = this
				for (let i = 0; i <= 10; i++) {
					let obj = {
						value: i,
						label: i
					}
					that.qualityList.push(obj)
				}
			},
			qualityConfirm(e) {
				let that = this
				that.form.quality = ""
				that.form.qualityCode = ""
				e.map((val, index) => {
					that.form.quality = val.label + "成新"
					that.form.qualityCode = val.value
					that.qualityDefault = [index]
				})
			},
			//获取导航栏
			getTab() {
				let that = this
				that.postRequest(helper.tabListUrl).then(res => {
					if (res.data.flag == "200") {
						let serve = res.data
						serve.tab.forEach(item => {
							let obj = {
								value: item.id,
								label: item.title
							}
							that.typeList.push(obj)
						})

					}
				})
			},
			typeConfirm(e) {
				let that = this
				that.form.type = ""
				that.form.typeCode = ""
				e.map((val, index) => {
					that.form.type = val.label
					that.form.typeCode = val.value
					that.typeDefault = [index]
				})
			},
			//提交
			sumbit() {
				let that = this
				let user = uni.getStorageSync("user")
				var file = []
				that.fileImage.forEach((item, index) => {
					file.push(item.url)
				})
				let data = {
					title: that.form.title,
					shopType: that.form.typeCode,
					content: that.form.content,
					original: that.form.original,
					price: that.form.price,
					shopNum: that.form.shopNum,
					quality: that.form.qualityCode,
					shopUrl: file.join("<<tupian>>"),
					userId: user.id
				}
				that.postRequest(helper.addShop, data).then(res => {
					if (res.data.flag == "200") {
						uni.showToast({
							title: '商品添加成功',
							icon: 'none',
							duration: 2000
						})
					} else {
						uni.showToast({
							title: '商品添加失败',
							icon: 'none',
							duration: 2000
						})
					}
				})
			},
			onSuccess(data, index, lists) {
				const that = this
				let currentFile = [];
				// console.log(data,lists)
				lists.forEach((item, index) => {
					if (item.response) {
						let obj = {
							url: item.response.collectNum
						}
						currentFile.push(obj)
					} else {
						let link = item.url
						let obj = {
							url: link
						}
						currentFile.push(obj)
					}
				})
				that.fileImage = currentFile
			},
			onRemove(data, lists) {
				const that = this
				if (lists.length > 0) {
					var currentfileList = [];
					lists.forEach((item, index) => {
						if (item.response) {
							let obj = {
								url: item.response.collectNum
							}
							currentfileList.push(obj)
						} else {
							let link = item.url
							let obj = {
								url: link
							}
							currentfileList.push(obj)
						}
					})
					that.fileImage = currentfileList
				} else {
					that.fileImage = []
				}
			}
		},
		watch: {
			fileList() {
				console.log(this.form.fileList)
			}
		}
	}
</script>

<style>

</style>
